package com.weipai.springboot.module.controller;

import com.alibaba.fastjson.JSON;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Role;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.model.api.SaveRoleMenusRequest;
import com.weipai.springboot.module.service.MenuService;
import com.weipai.springboot.module.service.RoleService;
import com.weipai.springboot.module.service.impl.MenuServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author songmengqiang
 * @create 2018-08-21 17:43
 **/

@RestController
public class
RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    /**  角色列表
     */
    @GetMapping("/role/list/{draw}")
    public Response list(@PathVariable("draw") String draw) {
        Response response = new Response();
        try {
            response = roleService.list(draw);
        } catch (Exception e) {
            logger.error("role list with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/role/save")
    public Response save(Role role) {
        Response response = new Response();
        try {
            response = roleService.save(role);
        } catch (Exception e) {
            logger.error("role save with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    /**
     * 查询角色
     *
     * @param id
     * @return
     */
    @GetMapping("/role/getRoleById/{id}")
    public Response getRole(@PathVariable Long id) {
        Response response = new Response();
        try {
            response = roleService.getRole(id);
        } catch (Exception e) {
            logger.error("role getRole with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //角色更新
    @PostMapping("/role/update")
    public Response update(Role role) {
        logger.info("into update {}", role);
        Response response = new Response();
        try {
            response = roleService.update(role);
        } catch (Exception e) {
            logger.error("role update with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //通过id 删除角色
    @DeleteMapping("/role/deleteById/{id}")
    public Response deleteById(@PathVariable("id") Long id) {
        logger.info("into deleteById {}", id);
        Response response = new Response();
        try {
            response = roleService.deleteById(id);
        } catch (Exception e) {
            logger.error("role deleteById with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //删除多个 角色
    @DeleteMapping("/role/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id") Long[] id) {
        logger.info("into batchDeleteById {}", Arrays.toString(id));
        Response response = new Response();
        try {
            response = roleService.batchDeleteById(id);
        } catch (Exception e) {
            logger.error("role batchDeleteById with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    /**
     * 根据角色id获得权限列表
     *
     * @param id
     * @return
     */
    @GetMapping("/role/getMenusByRoleId/{id}")
    public Response getMenusByRoleId(@PathVariable("id") Long id) {
        logger.info("into getMenusByRoleId {}", id);
        Response response = new Response();
        try {
            response = menuService.getMenusByRoleId(id);
        } catch (Exception e) {
            logger.error("role getMenusByRoleId with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }


    //根据角色id,保存 角色权限
    @PostMapping("/role/saveRoleMenus")
    public Response saveRoleMenus(String data) {
        SaveRoleMenusRequest request = JSON.parseObject(data, SaveRoleMenusRequest.class);
        logger.info("into saveRoleMenus {}", request);
        Response response = new Response();
        try {
            response = menuService.saveRoleMenus(request);
        } catch (Exception e) {
            logger.error("role saveRoleMenus with error {}", e);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
